import kivy

from kivy.uix.modalview import ModalView
from kivy.uix.textinput import TextInput
from kivy.animation import Animation
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.properties import ObjectProperty

Builder.load_string('''
<NumPad>:
    size_hint_y: .55
    size_hint_x: 1.05
    pos_hint: {'y': -0.7}
    GridLayout:
        size_hint: .95, .93
        cols:3
        Button:
            text: "7"
            size_hint: (.33,.25)
            on_release: root.button_callback("7")
            font_size: self.height * .4
        Button:
            text: "8"
            size_hint: (.33,.25)
            on_release: root.button_callback("8")
            font_size: self.height * .4
        Button:
            text: "9"
            size_hint: (.33,.25)
            on_release: root.button_callback("9")  
            font_size: self.height * .4          
        Button:
            text: "4"
            size_hint: (.33,.25)
            on_release: root.button_callback("4")
            font_size: self.height * .4
        Button:
            text: "5"
            size_hint: (.33,.25)
            on_release: root.button_callback("5")
            font_size: self.height * .4
        Button:
            text: "6"
            size_hint: (.33,.25)
            on_release: root.button_callback("6")
            font_size: self.height * .4
        Button:
            text: "1"
            size_hint: (.33,.25)
            on_release: root.button_callback("1")
            font_size: self.height * .4
        Button:
            text: "2"
            size_hint: (.33,.25)
            on_release: root.button_callback("2")
            font_size: self.height * .4
        Button:
            text: "3"
            size_hint: (.33,.25)
            on_release: root.button_callback("3")
            font_size: self.height * .4
        Button:
            text: "delete"
            size_hint: (.33,.25)
            on_release: root.button_callback("del")
            font_size: self.height * .4
        Button:
            text: "0"
            size_hint: (.33,.25)
            on_release: root.button_callback("0")
            font_size: self.height * .4
        Button:
            text: "enter"
            size_hint: (.33,.25)
            on_release: root.button_callback("ret")
            font_size: self.height * .4
''')

class NumInput(TextInput):
    adapter = ObjectProperty()
    
    def __init__(self, **kwargs):
        super(NumInput, self).__init__(**kwargs)
        self.numpad = NumPad(self)
        
    def on_focus(self, strange_object, focus): #second argument is realy strange
        if focus:
            self.adapter.hide_keyboard()
            self.numpad.open()
        else:
            self.numpad.dismiss()
            self.adapter.show_keyboard()            
            
class NumPad(ModalView):
    def __init__(self, root, **kwargs):
        self.root = root
        self.background_color = [0, 0, 0, 0]
        #self.root.presenter.adapter.hide_keyboard()
        super(NumPad, self).__init__(**kwargs)


    def button_callback(self, button_str):
        if button_str == 'del':
            self.root.text = self.root.text[:-1]
            return
        if button_str == 'ret':
            self.dismiss()
            return
        self.root.text += button_str

    def on_open(self):
        anim = Animation(pos_hint={'y': -.015}, duration = 0.3)
        anim.start(self)
        self.root.focus = True
        
    def dismiss(self):
        anim = Animation(pos_hint={'y': -0.7}, duration = 0.3)       
        anim.start(self)
        self.root.focus = False
        Clock.schedule_once(self.realy_dismiss, 0.3)
        
    def realy_dismiss(self, time):
        self._real_remove_widget()